package fr.upstlse.glre.client.administration.utilisateurs;

import java.util.List;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.binder.DataListBinder;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.DataList;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.administration.component.Center;
import fr.upstlse.glre.client.administration.utilisateurs.action.UserSelectionAction;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.user.UserVOGWT;
import fr.upstlse.glre.client.widgets.modelData.UserModelData;

public class ModificationPanel extends Composite {
	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	private ContentPanel modificationPanel;

	/**
	 * Panneau FORM contenant le profil au format formulaire pour l'édition.
	 */
	private DataList modificationList;

	private ContentPanel userModificationPanel;

	private List<UserVOGWT> userVOGWTs;

	private static ModificationPanel instance;

	public ModificationPanel() {
		ModificationPanel.setInstance(this);

		createModificationPanel();
		createUserModificationPanel();
		modificationList = new DataList();

		modificationPanel.add(modificationList);
		modificationPanel.add(userModificationPanel);
		initWidget(modificationPanel);
	}

	private void createModificationPanel() {
		modificationPanel = new ContentPanel();
		modificationPanel.setAutoWidth(true);
		modificationPanel.setWidth(500);
		modificationPanel
				.setId(RfgConstante.ADMIN_UTILISATEURS_MODIFICATION_PANEL_ID);
		modificationPanel.setBorders(true);
		modificationPanel.setCollapsible(true);
		modificationPanel.collapse();

		// Draggable panel
		Draggable lDraggable = new Draggable(modificationPanel,
				modificationPanel.getHeader());
		lDraggable.setContainer(Center.getInstance());

		modificationPanel
				.setHeading(RfgConstante.ADMIN_UTILISATEURS_MODIFICATION_PANEL_HEADER_TEXT);

		modificationPanel.addListener(Events.Expand, new Listener<BaseEvent>() {

			public void handleEvent(BaseEvent be) {
				createModificationList();
			}

		});
	}

	private void createModificationList() {
		if (userVOGWTs == null) {
			final MessageBox lAttente = MessageBox.alert("Information",
					"Chargement des utilisateurs en cours...", null);
			AsyncCallback<List<UserVOGWT>> lCallback = new AsyncCallback<List<UserVOGWT>>() {

				public void onFailure(Throwable pCaught) {
					lAttente.hide();
					MessageBox.alert("Erreur", "Erreur getAllUser "
							+ pCaught.getMessage(), null);
				}

				public void onSuccess(List<UserVOGWT> pResult) {
					lAttente.hide();
					userVOGWTs = pResult;

					final ListStore<UserModelData> lStore = new ListStore<UserModelData>();
					lStore.add(UserModelData.toUserModelDatas(userVOGWTs));
					DataListBinder<UserModelData> binder = new DataListBinder<UserModelData>(
							modificationList, lStore);
					binder
							.setDisplayProperty(RfgConstante.USER_MODELDATA_LOGIN);
					binder
							.setStringProvider(new ModelStringProvider<ModelData>() {

								public String getStringValue(ModelData model,
										String property) {
									String lTexte = (String) model
											.get(RfgConstante.USER_MODELDATA_LOGIN);
									lTexte += " ("
											+ (String) model
													.get(RfgConstante.USER_MODELDATA_ROLE)
											+ ")";
									return lTexte;
								}

							});
					binder.init();

					// final StoreFilter<ModelData> filter = new
					// StoreFilter<ModelData>() {
					//
					// public boolean select(Store store, ModelData parent,
					// ModelData item, String property) {
					// if (((String) item
					// .get(RfgConstante.DEPOT_MODELDATA_LIBELLE)).compareTo(
					// libelleCourant)
					// != 0){
					// return false;
					// } else {
					// return true;
					// }
					// }
					// };
					// lStore.addFilter(filter);

					modificationList.addListener(Events.SelectionChange,
							new UserSelectionAction());

					// final ComboBox lFilterComboBox = createFilterComboBox();
					// lFilterComboBox.addListener(Events.Select,
					// new FilterSelectionAction(Telecharger.getInstance(),
					// lStore, filter, lFilterComboBox));
					//
					//telechargerContentPanel.getHeader().addTool(lFilterComboBox
					// );
					// telechargerContentPanel.add(lFilterComboBox);
					Info.display("Information",
							"Succès du chargement des utilisateurs", "");
				}

			};
			ControlerUtil.getUserControlerServiceAsyncInstance().getAllUser(
					lCallback);
		}
	}

	private void createUserModificationPanel() {
		userModificationPanel = new ContentPanel();
		userModificationPanel.setAutoWidth(true);
		userModificationPanel.setWidth(500);
		userModificationPanel
				.setId(RfgConstante.ADMIN_UTILISATEURS_USER_MODIFICATION_PANEL_ID);
		userModificationPanel.setBorders(true);
		userModificationPanel.setHeaderVisible(false);
	}

	public ContentPanel getModificationPanel() {
		return modificationPanel;
	}

	public void setModificationPanel(ContentPanel modificationPanel) {
		this.modificationPanel = modificationPanel;
	}

	public DataList getModificationList() {
		return modificationList;
	}

	public void setModificationList(DataList modificationList) {
		this.modificationList = modificationList;
	}

	public ContentPanel getUserModificationPanel() {
		return userModificationPanel;
	}

	public void setUserModificationPanel(ContentPanel userModificationPanel) {
		this.userModificationPanel = userModificationPanel;
	}

	public List<UserVOGWT> getUserVOGWTs() {
		return userVOGWTs;
	}

	public void setUserVOGWTs(List<UserVOGWT> userVOGWTs) {
		this.userVOGWTs = userVOGWTs;
	}

	public static ModificationPanel getInstance() {
		if (instance == null) {
			instance = new ModificationPanel();
		}

		instance.getModificationPanel().layout();
		return instance;
	}

	public static void setInstance(ModificationPanel pModificationPanel) {
		instance = pModificationPanel;
	}
}
